package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TMStatMfdMchntDayMapper;
import cc.rengu.igas.momp.common.entity.TMStatMfdMchntDay;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 手续费减免活动统计表数据数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/20 22:31
 */

public class TMStatMfdMchntDayMapperImpl implements TMStatMfdMchntDayMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 使用主键查询手续费减免活动商户单日统计表数据数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex 规则顺序号
     * @param mchntNo   商户编号
     * @param txnDate   交易日期
     * @return cc.rengu.igas.momp.common.entity.TMStatMfdMchntDay 手续费减免活动商户单日统计表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/20 23:55
     */
    @Override
    public TMStatMfdMchntDay selectMchntFeeDiscountMchntDayStatisticsByPK(String instId, String mcNo, String ruleIndex, String mchntNo, String txnDate) throws Exception {

        TMStatMfdMchntDay tmStatMfdMchntDay = new TMStatMfdMchntDay();
        tmStatMfdMchntDay.setInstId(instId);
        tmStatMfdMchntDay.setMcNo(mcNo);
        tmStatMfdMchntDay.setRuleIndex(ruleIndex);
        tmStatMfdMchntDay.setMchntNo(mchntNo);
        tmStatMfdMchntDay.setTxnDate(txnDate);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeDiscountMchntDayStatisticsByPK");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdMchntDay, tmStatMfdMchntDay);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>的商户<{}>的商户手续费优惠活动<{}>的<{}>的规则编号<{}>的统计数据失败!<{}>",
                    instId, mchntNo, mcNo, txnDate, ruleIndex, iReturnCode);
            return null;
        }

        return tmStatMfdMchntDay;
    }

    /**
     * 回退手续费减免活动商户单日统计表数据数据库操作实现
     *
     * @param tmStatMfdMchntDay
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/23 19:43
     */
    @Override
    public int reverseMchntFeeDiscountMchntDayStatistics(TMStatMfdMchntDay tmStatMfdMchntDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "reverseMchntFeeDiscountMchntDayStatistics");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdMchntDay, TMStatMfdMchntDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("回退法人机构号<{}>的商户<{}>的商户手续费优惠活动<{}>的<{}>的规则编号<{}>的统计数据失败!<{}>", tmStatMfdMchntDay.getInstId(),
                    tmStatMfdMchntDay.getMchntNo(), tmStatMfdMchntDay.getMcNo(), tmStatMfdMchntDay.getTxnDate(), tmStatMfdMchntDay.getRuleIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 新增手续费减免活动商户单日统计表数据数据库操作实现
     *
     * @param tmStatMfdMchntDay 手续费减免活动商户单日统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/10 21:08
     */
    @Override
    public int insertMchntFeeDiscountMchntDayStatistics(TMStatMfdMchntDay tmStatMfdMchntDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntFeeDiscountMchntDayStatistics");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdMchntDay, TMStatMfdMchntDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("新增法人机构号<{}>的商户<{}>的商户手续费优惠活动<{}>的<{}>的规则编号<{}>的统计数据失败!<{}>", tmStatMfdMchntDay.getInstId(),
                    tmStatMfdMchntDay.getMchntNo(), tmStatMfdMchntDay.getMcNo(), tmStatMfdMchntDay.getTxnDate(), tmStatMfdMchntDay.getRuleIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 更新手续费减免活动商户单日统计表数据数据库操作实现
     *
     * @param tmStatMfdMchntDay 手续费减免活动商户单日统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/10 21:08
     */
    @Override
    public int updateMchntFeeDiscountMchntDayStatistics(TMStatMfdMchntDay tmStatMfdMchntDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntFeeDiscountMchntDayStatistics");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdMchntDay, TMStatMfdMchntDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新法人机构号<{}>的商户<{}>的商户手续费优惠活动<{}>的<{}>的规则编号<{}>的统计数据失败!<{}>", tmStatMfdMchntDay.getInstId(),
                    tmStatMfdMchntDay.getMchntNo(), tmStatMfdMchntDay.getMcNo(), tmStatMfdMchntDay.getTxnDate(), tmStatMfdMchntDay.getRuleIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }
}
